home *** CD-ROM | disk | FTP | other *** search
- Path: dd.chalmers.se!news.chalmers.se!sunic!trane.uninett.no!eunet.no!EU.net!howland.reston.ans.net!sol.ctr.columbia.edu!news.kei.com!bloom-beacon.mit.edu!senator-bedfellow.mit.edu!tada
- From: tada@ATHENA.MIT.EDU (Michael J Zehr)
- Newsgroups: comp.lang.c
- Subject: Re: Variable Length Structures ?
- Date: 22 Mar 1994 15:36:27 GMT
- Organization: Massachusetts Institute of Technology
- Lines: 40
- Message-ID: <2mn39r$ajs@senator-bedfellow.MIT.EDU>
- References: <Cn0qAy.7KB@cbfsb.cb.att.com> <ua302aa.764269592@sun2>
- NNTP-Posting-Host: alfredo.mit.edu
-
- In article <ua302aa.764269592@sun2> ua302aa@sun2.LRZ-Muenchen.DE (Kurt Watzka) writes:
- >ddewar@cbnewsb.cb.att.com (derek.a.dewar) writes:
- >>Does anyone out there in C-land know if it's at all possible to have
- >>a variable length structures in C ? Something like the following :
- >Try something like this:
- > struct blah {
- > /* fixed fields */
- > int num_fields;
- > int var[ 1 ];
- > }
- >
- >Then allocate memory like this
- > struct blah *p = (struct blah *)
- > malloc( sizeof( struct blah ) +
- > (num_fields - 1 ) * sizeof( int ) );
- >You can't successfully use sizeof on such a "variable length
- >structure". [or assignment]
-
-
-
- The C standards committee recently ruled that this is not strictly
- conforming and not guaranteed to work. While it will work on most
- platforms, one should be aware of this when using it.
-
- Naturally a C vendor is free to provide this as a language extension,
- but if they don't *say* they provide it, then they don't have to. (No,
- I'm not too happy about the situation either.)
-
- [The reasoning behind the restriction has to do with compilers that
- might do bounds checking based on the declared size of the array. One
- can also imagine segmented architectures for which the size of the
- struct is just below the size of a segment, but the extra space makes it
- larger than a segment, yet accesses to the array are generated by the
- compiler assuming that the array is in the same segment as the rest of
- the structure. I'm not claiming there *are* such architectures, only
- that there could be.]
-
- *sigh*
-
- -michael j zehr
-